import axios from "axios";
import { Toast } from "vant";
import   getToken   from "./getToken";

const request = axios.create({
    baseURL: "/api",
    timeout: 10000,
});

request.interceptors.request.use(
    (config) => {
        config.headers.token = getToken();
        return config;
    },
    (error) => {
        Toast.fail("请求发送失败" + error);
        return Promise.reject(error);
    }
);

request.interceptors.response.use(
    (response) => {
        console.log(response);
        
        if (response.data.code === 200) {
            return response.data;
        } else {
            Toast.fail("请求失败");
            return Promise.reject(response);
        }
    },
    (error) => {
        Toast.fail("请求失败" + error);
        return Promise.reject(error);
    }
);

export default request;

